<!DOCTYPE html>
<html lang="en-us" dir="ltr">
    <head><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content='DRF框架 常用命令用py, (python manage.py)
视图头参request, 必须返回response
针对Request，总结如下特性： 对Django中的HttpRequest进行了拓展 会根据请求头中的Content-Type，自动进行解析 无论前端发送的哪种格式的数据，都可以以相同的方式读取 request.data 类似于Django中的request.POST和request.FILES 可以对POST、PUT、PATCH的请求体参数进行解析 不仅支持form传参，也支持json格式传参 request.query_params 类似于Django中的request.GET 获取查询字符串参数 支持Django HttpRequest中所有的对象和方法
Django模型类 ​	元类库管名,主键默id ddd
​	表单blank null库空, 两表关联靠外键, 取值空值主键值
增 删 改 查
一种方式save对象, M.O.create增加值 ,
修改查找 up 嘚 , 删除先查再嘚类
filter get exclude, 过滤单个和排除, 属性两划比较等, gt大于lt小, 比较F逻辑Q
1 2 3 4 5 6 7 8 9 10 11 12 PeopleInfo.objects.create(name=&amp;#39;itheima&amp;#39;,book=book) PeopleInfo.objects.filter(name=&amp;#39;cast&amp;#39;).update(name=&amp;#39;播客&amp;#39;) &amp;gt;&amp;gt;&amp;gt; BookInfo.objects.filter(name=&amp;#39;python入门&amp;#39;).delete() BookInfo.objects.filter(id__exact=1) BookInfo.objects.filter(name__contains=&amp;#39;传&amp;#39;) BookInfo.objects.filter(name__startswith=&amp;#39;射&amp;#39;) BookInfo.objects.filter(name__isnull=True) BookInfo.objects.filter(name__in=[1,3,5]) BookInfo.objects.filter(id__gte=3) &amp;gt;&amp;gt;&amp;gt; BookInfo.'><title>序列化器serizalizer</title>

<link rel='canonical' href='http://meruro.cn/p/%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8serizalizer/'>

<link rel="stylesheet" href="/scss/style.min.5a8e892f6fa14515e9065eee1f5d2e05302606ec7f11750b2fb95198f9cdcbfd.css"><meta property='og:title' content='序列化器serizalizer'>
<meta property='og:description' content='DRF框架 常用命令用py, (python manage.py)
视图头参request, 必须返回response
针对Request，总结如下特性： 对Django中的HttpRequest进行了拓展 会根据请求头中的Content-Type，自动进行解析 无论前端发送的哪种格式的数据，都可以以相同的方式读取 request.data 类似于Django中的request.POST和request.FILES 可以对POST、PUT、PATCH的请求体参数进行解析 不仅支持form传参，也支持json格式传参 request.query_params 类似于Django中的request.GET 获取查询字符串参数 支持Django HttpRequest中所有的对象和方法
Django模型类 ​	元类库管名,主键默id ddd
​	表单blank null库空, 两表关联靠外键, 取值空值主键值
增 删 改 查
一种方式save对象, M.O.create增加值 ,
修改查找 up 嘚 , 删除先查再嘚类
filter get exclude, 过滤单个和排除, 属性两划比较等, gt大于lt小, 比较F逻辑Q
1 2 3 4 5 6 7 8 9 10 11 12 PeopleInfo.objects.create(name=&amp;#39;itheima&amp;#39;,book=book) PeopleInfo.objects.filter(name=&amp;#39;cast&amp;#39;).update(name=&amp;#39;播客&amp;#39;) &amp;gt;&amp;gt;&amp;gt; BookInfo.objects.filter(name=&amp;#39;python入门&amp;#39;).delete() BookInfo.objects.filter(id__exact=1) BookInfo.objects.filter(name__contains=&amp;#39;传&amp;#39;) BookInfo.objects.filter(name__startswith=&amp;#39;射&amp;#39;) BookInfo.objects.filter(name__isnull=True) BookInfo.objects.filter(name__in=[1,3,5]) BookInfo.objects.filter(id__gte=3) &amp;gt;&amp;gt;&amp;gt; BookInfo.'>
<meta property='og:url' content='http://meruro.cn/p/%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8serizalizer/'>
<meta property='og:site_name' content='梅鲁梅鲁梅鲁梅~'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:tag' content='代码' /><meta property='article:published_time' content='2019-07-18T22:00:07&#43;00:00'/><meta property='article:modified_time' content='2019-07-18T22:00:07&#43;00:00'/>
<meta name="twitter:title" content="序列化器serizalizer">
<meta name="twitter:description" content="DRF框架 常用命令用py, (python manage.py)
视图头参request, 必须返回response
针对Request，总结如下特性： 对Django中的HttpRequest进行了拓展 会根据请求头中的Content-Type，自动进行解析 无论前端发送的哪种格式的数据，都可以以相同的方式读取 request.data 类似于Django中的request.POST和request.FILES 可以对POST、PUT、PATCH的请求体参数进行解析 不仅支持form传参，也支持json格式传参 request.query_params 类似于Django中的request.GET 获取查询字符串参数 支持Django HttpRequest中所有的对象和方法
Django模型类 ​	元类库管名,主键默id ddd
​	表单blank null库空, 两表关联靠外键, 取值空值主键值
增 删 改 查
一种方式save对象, M.O.create增加值 ,
修改查找 up 嘚 , 删除先查再嘚类
filter get exclude, 过滤单个和排除, 属性两划比较等, gt大于lt小, 比较F逻辑Q
1 2 3 4 5 6 7 8 9 10 11 12 PeopleInfo.objects.create(name=&amp;#39;itheima&amp;#39;,book=book) PeopleInfo.objects.filter(name=&amp;#39;cast&amp;#39;).update(name=&amp;#39;播客&amp;#39;) &amp;gt;&amp;gt;&amp;gt; BookInfo.objects.filter(name=&amp;#39;python入门&amp;#39;).delete() BookInfo.objects.filter(id__exact=1) BookInfo.objects.filter(name__contains=&amp;#39;传&amp;#39;) BookInfo.objects.filter(name__startswith=&amp;#39;射&amp;#39;) BookInfo.objects.filter(name__isnull=True) BookInfo.objects.filter(name__in=[1,3,5]) BookInfo.objects.filter(id__gte=3) &amp;gt;&amp;gt;&amp;gt; BookInfo.">
    </head>
    <body class="
    article-page
    ">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex on-phone--column extended"><aside class="sidebar left-sidebar sticky ">
    <button class="hamburger hamburger--spin" type="button" id="toggle-menu" aria-label="Toggle Menu">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>

    <header>
        
            
            <figure class="site-avatar">
                <a href="/">
                
                    
                    
                    
                        
                        <img src="/img/ava_hu31add2dceb44736f969f74989490346b_19511_300x0_resize_q75_box.jpg" width="300"
                            height="300" class="site-logo" loading="lazy" alt="Avatar">
                    
                
                </a>
                
                    <span class="emoji">🍥</span>
                
            </figure>
            
        
        
        <div class="site-meta">
            <h1 class="site-name"><a href="/">梅鲁梅鲁梅鲁梅~</a></h1>
            <h2 class="site-description">朝闻道,夕死可矣.</h2>
        </div>
    </header><ol class="social-menu">
            
                <li>
                    <a 
                        href='https://github.com/CaiJimmy/hugo-theme-stack'
                        target="_blank"
                        title="GitHub"
                    >
                        
                        
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-github" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5" />
</svg>



                        
                    </a>
                </li>
            
                <li>
                    <a 
                        href='https://twitter.com'
                        target="_blank"
                        title="Twitter"
                    >
                        
                        
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-twitter" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M22 4.01c-1 .49 -1.98 .689 -3 .99c-1.121 -1.265 -2.783 -1.335 -4.38 -.737s-2.643 2.06 -2.62 3.737v1c-3.245 .083 -6.135 -1.395 -8 -4c0 0 -4.182 7.433 4 11c-1.872 1.247 -3.739 2.088 -6 2c3.308 1.803 6.913 2.423 10.034 1.517c3.58 -1.04 6.522 -3.723 7.651 -7.742a13.84 13.84 0 0 0 .497 -3.753c-.002 -.249 1.51 -2.772 1.818 -4.013z" />
</svg>



                        
                    </a>
                </li>
            
        </ol><ol class="menu" id="main-menu">
        
        
        

        <li >
            <a href='/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-home" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="5 12 3 12 12 3 21 12 19 12" />
  <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
</svg>



                
                <span>Home</span>
            </a>
        </li>
        
        

        <li >
            <a href='/about/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="7" r="4" />
  <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" />
</svg>



                
                <span>About</span>
            </a>
        </li>
        
        

        <li >
            <a href='/archives/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <rect x="3" y="4" width="18" height="4" rx="2" />
  <path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" />
  <line x1="10" y1="12" x2="14" y2="12" />
</svg>



                
                <span>Archives</span>
            </a>
        </li>
        
        

        <li >
            <a href='/search/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="10" cy="10" r="7" />
  <line x1="21" y1="21" x2="15" y2="15" />
</svg>



                
                <span>Search</span>
            </a>
        </li>
        
        

        <li >
            <a href='/links/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-link" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" />
  <path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" />
</svg>



                
                <span>Links</span>
            </a>
        </li>
        

        <div class="menu-bottom-section">
                <li id="i18n-switch">  
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-language" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M4 5h7" />
  <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
  <path d="M5 9c-.003 2.144 2.952 3.908 6.7 4" />
  <path d="M12 20l4 -9l4 9" />
  <path d="M19.1 18h-6.2" />
</svg>



                    <select name="language" onchange="window.location.href = this.selectedOptions[0].value">
                        
                            <option value="http://meruro.cn/" selected>English</option>
                        
                            <option value="http://meruro.cn/zh-cn/" >中文</option>
                        
                            <option value="http://meruro.cn/ar/" >عربي</option>
                        
                    </select>
                </li>
            
            
            
                <li id="dark-mode-toggle">
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="8" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-right" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="16" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                    <span>Dark Mode</span>
                </li>
            
        </div>
    </ol>
</aside>
<main class="main full-width">
    <article class="main-article">
    <header class="article-header">

    <div class="article-details">
    

    <div class="article-title-wrapper">
        <h2 class="article-title">
            <a href="/p/%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8serizalizer/">序列化器serizalizer</a>
        </h2>
    
        
    </div>

    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Jul 18, 2019</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    2 minute read
                </time>
            </div>
        
    </footer>
    

    
</div>
</header>

    <section class="article-content">
    
    
    <h1 id="drf框架">DRF框架</h1>
<p>常用命令用py, (python manage.py)</p>
<p>视图头参request, 必须返回response</p>
<h2 id="针对request总结如下特性">针对Request，总结如下特性：</h2>
<p>对Django中的HttpRequest进行了拓展
会根据请求头中的Content-Type，自动进行解析
无论前端发送的哪种格式的数据，都可以以相同的方式读取
request.data
类似于Django中的request.POST和request.FILES
可以对POST、PUT、PATCH的请求体参数进行解析
不仅支持form传参，也支持json格式传参
request.query_params
类似于Django中的request.GET
获取查询字符串参数
支持Django HttpRequest中所有的对象和方法</p>
<h3 id="django模型类">Django模型类</h3>
<p>​	元类库管名,主键默id ddd</p>
<p>​	表单blank null库空,  两表关联靠外键, 取值空值主键值</p>
<p>增 删 改 查</p>
<p>一种方式save对象, M.O.create增加值 ,</p>
<p>修改查找 up 嘚 , 删除先查再嘚类</p>
<p>filter get exclude, 过滤单个和排除,  属性两划比较等, gt大于lt小, 比较F逻辑Q</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">PeopleInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;itheima&#39;</span><span class="p">,</span><span class="n">book</span><span class="o">=</span><span class="n">book</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">PeopleInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;cast&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;播客&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;python入门&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">id__exact</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__contains</span><span class="o">=</span><span class="s1">&#39;传&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__startswith</span><span class="o">=</span><span class="s1">&#39;射&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__isnull</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">name__in</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
</span></span><span class="line"><span class="cl"><span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">id__gte</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">readcount__gt</span><span class="o">=</span><span class="n">F</span><span class="p">(</span><span class="s1">&#39;commentcount&#39;</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">Q</span><span class="p">(</span><span class="n">readcount__gt</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span><span class="o">|</span><span class="n">Q</span><span class="p">(</span><span class="n">id__lt</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">~</span><span class="n">Q</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>聚合和排序</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">Sum</span><span class="p">(</span><span class="s1">&#39;readcount&#39;</span><span class="p">))</span>
</span></span><span class="line"><span class="cl"><span class="o">&gt;&gt;&gt;</span> <span class="n">BookInfo</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="s1">&#39;readcount&#39;</span><span class="p">)</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol>
<li>
<p>使用<strong>MySQL</strong>数据库首先需要安装驱动程序</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">pip install PyMySQL
</span></span></code></pre></td></tr></table>
</div>
</div></li>
<li>
<p>在Django的工程同名子目录的__init__.py文件中添加如下语句</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">import pymysql
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">pymysql.install_as_MySQLdb()
</span></span></code></pre></td></tr></table>
</div>
</div><p>作用是让Django的ORM能以mysqldb的方式来调用PyMySQL。</p>
</li>
<li>
<p>修改<strong>DATABASES</strong>配置信息</p>
</li>
<li>
<p>在MySQL中创建数据库</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">create database book charset=utf8;
</span></span></code></pre></td></tr></table>
</div>
</div></li>
</ol>
<h3 id="django视图">django视图</h3>
<p>None有值null为空</p>
<h3 id="django中的序列化器">django中的序列化器</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">import json
</span></span><span class="line"><span class="cl">from django.core import serializers
</span></span><span class="line"><span class="cl">json_data = serializers.serialize(&#39;json&#39;,goods)
</span></span><span class="line"><span class="cl">json_data = json.loads(json_data)
</span></span><span class="line"><span class="cl">return JsonResponse(json_data,safe=False)
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="django中的模型类">django中的模型类</h3>
<p>​	一对多关系属性定义在多的那个类中,一对多,多对多关系属性定义在哪个类中都可以</p>
<h3 id="drf中的序列化器">DRF中的序列化器</h3>
<p>​	两个关键字 many=True当查询结果为数组或包含多条数据的查询集需要加many=True,  Read_on=True 该字段只可序列化不可反序列化时添加此字段
​	request中 .data属性包含所有的内容 .query_params包含get请求的参数 .parsers调用需要的解析器 .user
​	response中支持各种类型的返回 .data返回数据 .status状态码 .template_name=html指明返回html .header .content_type
​</p>
<h3 id="drf中的filter过滤器">drf中的filter过滤器</h3>
<p>​	BookInfo.object.get(heroinfo__hcomment__contains=&lsquo;八&rsquo;)#查询包含八的字段
​	BookInfo.object.get(heroinfo__id__gt=3)#查询id&gt;3的字段
​	写关联查询条件时,如果类中没有关联属性条件直接对应类名,有关联属性,写关联属性名
​	Q对象:与&amp;或|非~ (Q(id_gt=3)|Q(id2_gt=30)) 
F对象:用于类属性之间进行比较 gt=F(&lsquo;comment&rsquo;)
​	聚合函数aggregate,count,</p>
<h3 id="序列化器的save方法">序列化器的save()方法</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">serializer</span><span class="o">.</span><span class="n">save</span><span class="p">()</span><span class="c1">#下为源码</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">validated_data</span><span class="p">)</span><span class="c1">#此为校验过的数据</span>
</span></span><span class="line"><span class="cl"><span class="k">else</span><span class="p">:</span>
</span></span><span class="line"><span class="cl"><span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">instance</span><span class="p">,</span> <span class="n">validated_data</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>instance为序列化器的一个实例</p>
<h3 id="jwt原理">jwt原理</h3>
<p>用点连接分三段, 一段hearder类算法, 二段内容三加密</p>
<p>jwt生成的字符串有三段,中间用.连接 例如:dagkgsadh.dkasjhgdg.nbzxc</p>
<p>jwt
header头部包含两个信息,token的类型typ和采用的加密算法alg
payload负载:存放信息
signature签名</p>
<ol>
<li>第一段字符串:HEADER,内部包含算法和token类型<!-- raw HTML omitted --></li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">{
</span></span><span class="line"><span class="cl">	&#34;alg&#34;:&#34;HS256&#34;,#算法
</span></span><span class="line"><span class="cl">	&#34;typ&#34;:&#34;JWT&#34;#token类型
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>将此json转化为字符串,然后用base64url加密,产生第一段字符串,此加密可以反解
2. 第二段字符串:payload,自定义值,同样用base64加密</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">	<span class="s2">&#34;id&#34;</span><span class="p">:</span><span class="s2">&#34;12345&#34;</span>
</span></span><span class="line"><span class="cl">	<span class="s2">&#34;name&#34;</span><span class="p">:</span><span class="s2">&#34;zby&#34;</span>
</span></span><span class="line"><span class="cl">	<span class="s2">&#34;exp&#34;</span><span class="p">:</span><span class="s2">&#34;12312414&#34;</span><span class="c1">#超时时间</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ol start="3">
<li>第三段字符串:1,将前两串字符串拼接起来 2,对前两部分密文进行hs256加密+加盐(放入随机字符串) 3,对SHA256加密后的密文再进行base64加密
以后用户再来访问时需要携带token,后端对token进行处理</li>
<li>对token进行切割,分为三串字符串</li>
<li>对第二段进行解密,获取payload信息,判断token是否超时</li>
<li>把一二段拼接起来,再以同样方式进行hs256加密,将此次加密结果如用户传来的token比较,如果相同则token未被修改过</li>
</ol>
<h3 id="genericapiview通用视图">GenericAPIView通用视图</h3>
<p>此类下可以自定义get,post等方法,未定义的方法不可以访问</p>
<h3 id="小程序网络请求api">小程序网络请求api</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-JavaScript" data-lang="JavaScript"><span class="line"><span class="cl"><span class="nx">wx</span><span class="p">.</span><span class="nx">request</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">	<span class="nx">url</span><span class="o">:</span><span class="s1">&#39;http://127.0.0.1:8000/login&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">data</span><span class="o">:</span><span class="p">(</span><span class="nx">phone</span><span class="o">:</span><span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">.</span><span class="nx">phone</span><span class="p">,</span> <span class="nx">passwd</span><span class="o">:</span><span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">.</span><span class="nx">passwd</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">	<span class="nx">method</span><span class="o">:</span><span class="s1">&#39;POST&#39;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">	<span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">res</span><span class="p">){</span>
</span></span><span class="line"><span class="cl">		<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">res</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">	<span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">})</span>
</span></span><span class="line"><span class="cl"><span class="c1">//使用wx.request时需要注意-网络地址必须为https,-后台必须设置要访问的域名
</span></span></span></code></pre></td></tr></table>
</div>
</div><h3 id="js中this指向调用它的对象">js中this指向调用它的对象</h3>

</section>


    <footer class="article-footer">
    
    <section class="article-tags">
        
            <a href="/tags/%E4%BB%A3%E7%A0%81/">代码</a>
        
    </section>


    
    <section class="article-copyright">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copyright" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <path d="M14.5 9a3.5 4 0 1 0 0 6" />
</svg>



        <span>Licensed under CC BY-NC-SA 4.0</span>
    </section>
    </footer>


    
</article>

    

    

     
    
        
    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
            2020 - 
        
        2023 梅鲁梅鲁梅鲁梅~
    </section>
    
    <section class="powerby">
        Built with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> <br />
        Theme <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.13.0">Stack</a></b> designed by <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a>
    </section>
</footer>


    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css"crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"crossorigin="anonymous"
            >

            </main>
    <aside class="sidebar right-sidebar sticky">
        
            
                
    <section class="widget archives">
        <div class="widget-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <line x1="5" y1="9" x2="19" y2="9" />
  <line x1="5" y1="15" x2="19" y2="15" />
  <line x1="11" y1="4" x2="7" y2="20" />
  <line x1="17" y1="4" x2="13" y2="20" />
</svg>



        </div>
        <h2 class="widget-title section-title">Table of contents</h2>
        
        <div class="widget--toc">
            <nav id="TableOfContents">
  <ol>
    <li><a href="#针对request总结如下特性">针对Request，总结如下特性：</a>
      <ol>
        <li><a href="#django模型类">Django模型类</a></li>
        <li><a href="#django视图">django视图</a></li>
        <li><a href="#django中的序列化器">django中的序列化器</a></li>
        <li><a href="#django中的模型类">django中的模型类</a></li>
        <li><a href="#drf中的序列化器">DRF中的序列化器</a></li>
        <li><a href="#drf中的filter过滤器">drf中的filter过滤器</a></li>
        <li><a href="#序列化器的save方法">序列化器的save()方法</a></li>
        <li><a href="#jwt原理">jwt原理</a></li>
        <li><a href="#genericapiview通用视图">GenericAPIView通用视图</a></li>
        <li><a href="#小程序网络请求api">小程序网络请求api</a></li>
        <li><a href="#js中this指向调用它的对象">js中this指向调用它的对象</a></li>
      </ol>
    </li>
  </ol>
</nav>
        </div>
    </section>

            
        
    </aside>


        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.6/dist/vibrant.min.js"integrity="sha256-awcR2jno4kI5X0zL8ex0vi2z&#43;KMkF24hUW8WePSA9HM="crossorigin="anonymous"
                
                >
            </script><script type="text/javascript" src="/ts/main.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>

    </body>
</html>
